package com.example.salesystem.service.impl;

import com.example.salesystem.common.ApplicationException;
import com.example.salesystem.common.ResultAjax;
import com.example.salesystem.model.User;
import com.example.salesystem.mapper.UserMapper;
import com.example.salesystem.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lishuo
 * @since 2024-05-18
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    /**
     * 用户登录
     *
     * @param username
     * @param password
     */
    @Override
    public User login(String username, String password) {
        // 1.根据用户名查询用户是否存在
        User user = lambdaQuery().eq(User::getAccount, username).one();
        if (user == null) {
            log.warn("用户名或密码错误");
            throw new ApplicationException(ResultAjax.fail("用户名或密码错误"));
        }
        // 2. 校验密码是否正确
        if (!password.equals(user.getPassword())) {
            log.warn("用户名或密码错误");
            throw new ApplicationException(ResultAjax.fail("用户名或密码错误"));
        }
        // 3.登录成功，返回用户信息
        // 打印日志
        log.info("登录成功：username = " + username);
        return user;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public User modefyInfo(User user) {
        System.out.println(user);

        // 1. 查询用户是否存在
        User oldUser = getById(user.getId());
        if (oldUser == null) {
            log.warn("用户不存在");
            throw new ApplicationException(ResultAjax.fail("用户不存在"));
        }
        // 2. 修改
        boolean update = lambdaUpdate()
                .set(user.getUsername() != null, User::getUsername, user.getUsername())
                .set(user.getEmail() != null, User::getEmail, user.getEmail())
                .set(user.getPhone() != null, User::getPhone, user.getPhone())
                .set(user.getAddress() != null, User::getAddress, user.getAddress())
                .eq(User::getId, user.getId())
                .update();
        if (!update) {
            log.warn("更新失败");
            throw new ApplicationException(ResultAjax.fail("更新失败"));
        }
        // 3. 返回最新结果
        return lambdaQuery().eq(User::getId, user.getId()).one();
    }

    /**
     * 修改密码
     *
     * @param oldPassword
     * @param newPassword
     * @param oldUser
     */
    @Override
    public void modefyPwd(String oldPassword, String newPassword, User oldUser) {
        // 1.校验原秘密是否正确
        if (!oldPassword.equals(oldUser.getPassword())) {
            log.warn("原密码错误");
            throw new ApplicationException(ResultAjax.fail("原密码错误"));
        }
        // 2. 更新密码
        boolean update = lambdaUpdate().set(User::getPassword, newPassword).eq(User::getId, oldUser.getId()).update();
        if (!update) {
            log.warn("更新失败");
            throw new ApplicationException(ResultAjax.fail("更新失败"));
        }
    }

    @Override
    public String realMethod() {
        System.out.println("service-realeMethod!");
        return ("service-realMethod!");
    }

}
